import {getCurrentInstance} from 'vue'

export function useRect() {
    const instance = getCurrentInstance();
    const getRect = (selector: string, all?: boolean): Promise<UniApp.NodeInfo | UniApp.NodeInfo[]> => {
        return new Promise((resolve) => {
            uni.createSelectorQuery()
                .in(instance?.proxy)
                [all ? 'selectAll' : 'select'](selector)
                .boundingClientRect(rect => {
                    if (all && Array.isArray(rect) && rect.length) {
                        resolve(rect)
                    }
                    if (!all && rect) {
                        resolve(rect)
                    }
                })
                .exec()
        })
    }

    return {
        getRect
    }
}